package com.nuoniu.sibanyun.controller.finance;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.finance.VAccountingLedger;
import com.nuoniu.sibanyun.entity.finance.VAssetsLiabilitiesFormula;
import com.nuoniu.sibanyun.entity.finance.VAssetsLiabilitiesMain;
import com.nuoniu.sibanyun.service.finance.IVAssetsLiabilitiesMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 资产负债表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/finance/assetsLiabilities")
@Api(tags = "资产负债表管理")
public class VAssetsLiabilitiesMainController extends BaseController {

    @Autowired
    private IVAssetsLiabilitiesMainService assetsLiabilitiesMainService;

    /**
     * 保存负债表
     * @author 小啊刚
     * @date 2021-05-26
     */
    @ApiOperation("保存负债表")
    @PostMapping("save")
    public HttpResult<VAssetsLiabilitiesMain> save(@RequestBody List<VAssetsLiabilitiesMain> assetsLiabilities) {
        if(assetsLiabilities == null || assetsLiabilities.size() == 0) {
            throw new NuoNiuException("负债不能为空");
        }
        
        assetsLiabilitiesMainService.saveAssetsLiabilities(assetsLiabilities);
        return HttpResult.ok();
    }

    /**
     * 获取负债表
     * @author 小啊刚
     * @date 2021-05-27
     */
    @ApiOperation("获取负债表")
    @GetMapping("getAssetsLiabilities")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "accountPeriod", value = "期间 0表示年")
    })
    public HttpResult<VAssetsLiabilitiesMain> getAssetsLiabilities(Integer accountPeriod, Integer type) {
        if(type == null) {
            return HttpResult.error("类型不能为空");
        }
        return HttpResult.ok(assetsLiabilitiesMainService.getAssetsLiabilities(accountPeriod, type));
    }

    /**
     * 获取公式列表
     * @author 小啊刚
     * @date 2021-05-28
     */
    @ApiOperation("获取公式列表")
    @GetMapping("getFormulaList")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "alId", value = "资产负债id")
    })
    public HttpResult<VAssetsLiabilitiesFormula> getFormulaList(Long alId) {
        return HttpResult.ok(assetsLiabilitiesMainService.getFormulaList(alId));
    }

    /**
     * 获取科目总账
     * @author 小啊刚
     * @date 2021-05-28
     */
    @ApiOperation("获取科目总账")
    @GetMapping("getAccountingLedger")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "科目编码"),
        @ApiImplicitParam(name = "accountPeriod", value = "账套期间")
    })
    public HttpResult<VAccountingLedger> getAccountingLedger(String code) {
        return HttpResult.ok(assetsLiabilitiesMainService.getAccountingLedger(code));
    }

    /**
     * 保存公式
     * @author 小啊刚
     * @date 2021-06-25
     */
    @ApiOperation("保存公式")
    @PostMapping("saveFormula")
    public HttpResult saveFormula(@RequestBody List<VAssetsLiabilitiesMain> assetsLiabilities) {
        assetsLiabilitiesMainService.saveFormula(assetsLiabilities);
        return HttpResult.ok();
    }

    /**
     * 导出报表
     * @author 小啊刚
     * @date 2022-06-11
     */
    @ApiOperation("导出报表")
    @GetMapping("exportData")
    public void exportData(Integer accountYear, Integer accountPeriod, Integer type) {
        if(type == null) {
            throw new NuoNiuException("类型不能为空");
        }

        assetsLiabilitiesMainService.exportData(accountYear, accountPeriod, type);
    }
}
